import React from 'react';
import { message } from 'antd';
import { useCityList } from '@/pages/Organization/Job/hooks';
import { getSalaryRef } from '@/services/setting/salaryReference';

// const mockData = [
//     {
//         serialVersionUID: {},
//         id: 213,
//         salaryStandardName: '美世',
//         postTypeName: '技术研发-测试',
//         postName: '高级测试专家',
//         postCode: 'MS.007.008.P9',
//         currencyName: '人民币',
//         currency: 'RMB',
//         firstLineNineQuantile: 330000,
//         firstLineSevenQuantile: 290000,
//         firstLineFiveQuantile: 278000,
//         firstLineTwoQuantile: 270000,
//         secondLineNineQuantile: 300000,
//         secondLineSevenQuantile: 260000,
//         secondLineFiveQuantile: 258000,
//         secondLineTwoQuantile: 250000,
//         standardNamePostCode: 's1_MS.007.008.P9',
//         referencePostInfo: '',
//         referenceStandardName: '',
//         referencePostCode: '',
//         referenceStandardNameSecond: '',
//         referencePostCodeSecond: '',
//         referenceStandardNameThird: '',
//         referencePostCodeThird: '',
//         referenceStandardNameFourth: '',
//         referencePostCodeFourth: '',
//         referenceStandardNameFifth: '',
//         referencePostCodeFifth: '',
//         remark: ''
//     },
//     {
//         serialVersionUID: {},
//         id: 263,
//         salaryStandardName: '微微',
//         postTypeName: '技术研发-测试',
//         postName: '高级测试专家',
//         postCode: 'VV.007.008.P9',
//         currencyName: '人民币',
//         currency: 'RMB',
//         firstLineNineQuantile: 330000,
//         firstLineSevenQuantile: 280000,
//         firstLineFiveQuantile: 240000,
//         firstLineTwoQuantile: 200000,
//         secondLineNineQuantile: 300000,
//         secondLineSevenQuantile: 250000,
//         secondLineFiveQuantile: 200000,
//         secondLineTwoQuantile: 180000,
//         standardNamePostCode: 's2_VV.007.008.P9',
//         referencePostInfo:
//             '[{"id":213,"salaryStandardName":"美世","postTypeName":"技术研发-测试","postName":"高级测试专家","postCode":"MS.007.008.P9","currency":"RMB","currencyName":null,"firstLineNineQuantile":330000.00,"firstLineSevenQuantile":290000.00,"firstLineFiveQuantile":278000.00,"firstLineTwoQuantile":270000.00,"secondLineNineQuantile":300000.00,"secondLineSevenQuantile":260000.00,"secondLineFiveQuantile":258000.00,"secondLineTwoQuantile":250000.00,"standardNamePostCode":"s1_MS.007.008.P9","referencePostInfo":null,"referenceStandardName":"","referencePostCode":"","referenceStandardNameSecond":"","referencePostCodeSecond":"","referenceStandardNameThird":"","referencePostCodeThird":"","referenceStandardNameFourth":"","referencePostCodeFourth":"","referenceStandardNameFifth":"","referencePostCodeFifth":"","salaryStandardNameSort":2,"remark":null,"expandFlag":null},{"id":316,"salaryStandardName":"对标公司","postTypeName":"技术研发-测试","postName":"高级测试专家","postCode":"DUIBIAO.007.008.P9","currency":"RMB","currencyName":null,"firstLineNineQuantile":400000.00,"firstLineSevenQuantile":340000.00,"firstLineFiveQuantile":320000.00,"firstLineTwoQuantile":300000.00,"secondLineNineQuantile":280000.00,"secondLineSevenQuantile":240000.00,"secondLineFiveQuantile":220000.00,"secondLineTwoQuantile":200000.00,"standardNamePostCode":"s3_DUIBIAO.007.008.P9","referencePostInfo":null,"referenceStandardName":"","referencePostCode":"","referenceStandardNameSecond":"","referencePostCodeSecond":"","referenceStandardNameThird":"","referencePostCodeThird":"","referenceStandardNameFourth":"","referencePostCodeFourth":"","referenceStandardNameFifth":"","referencePostCodeFifth":"","salaryStandardNameSort":3,"remark":null,"expandFlag":null}]',
//         referenceStandardName: '美世',
//         referencePostCode: 'MS.007.008.P9',
//         referenceStandardNameSecond: '对标公司',
//         referencePostCodeSecond: 'DUIBIAO.007.008.P9',
//         referenceStandardNameThird: '',
//         referencePostCodeThird: '',
//         referenceStandardNameFourth: '',
//         referencePostCodeFourth: '',
//         referenceStandardNameFifth: '',
//         referencePostCodeFifth: '',
//         remark: '微微参考美世'
//     },
//     {
//         serialVersionUID: {},
//         id: 316,
//         salaryStandardName: '对标公司',
//         postTypeName: '技术研发-测试',
//         postName: '高级测试专家',
//         postCode: 'DUIBIAO.007.008.P9',
//         currencyName: '人民币',
//         currency: 'RMB',
//         firstLineNineQuantile: 400000,
//         firstLineSevenQuantile: 340000,
//         firstLineFiveQuantile: 320000,
//         firstLineTwoQuantile: 300000,
//         secondLineNineQuantile: 280000,
//         secondLineSevenQuantile: 240000,
//         secondLineFiveQuantile: 220000,
//         secondLineTwoQuantile: 200000,
//         standardNamePostCode: 's3_DUIBIAO.007.008.P9',
//         referencePostInfo: '',
//         referenceStandardName: '',
//         referencePostCode: '',
//         referenceStandardNameSecond: '',
//         referencePostCodeSecond: '',
//         referenceStandardNameThird: '',
//         referencePostCodeThird: '',
//         referenceStandardNameFourth: '',
//         referencePostCodeFourth: '',
//         referenceStandardNameFifth: '',
//         referencePostCodeFifth: '',
//         remark: ''
//     }
// ];

/**
 * 获取当前使用城市的薪资参考数据...数据结构非常怪
 * @param {*} data 参考数据
 * @param {*} cityTier 几线城市
 * @param {*} quantile 分位
 */
const getSalaryValue = (data, cityTier) => {
    const qMap = {
        1: {
            q25: 'firstLineTwoQuantile',
            q50: 'firstLineFiveQuantile',
            q75: 'firstLineSevenQuantile'
        },
        2: {
            q25: 'secondLineTwoQuantile',
            q50: 'secondLineFiveQuantile',
            q75: 'secondLineSevenQuantile'
        }
    };
    return {
        q25: data?.[qMap[cityTier]?.q25] ?? 0,
        q50: data?.[qMap[cityTier]?.q50] ?? 0,
        q75: data?.[qMap[cityTier]?.q75] ?? 0
    };
};

const getSelectedSource = dataSource => {
    const weightOrder = ['微微', '美世', '对标公司'];
    const sortedDataSource = weightOrder
        .map(companyName => dataSource.find(it => it.salaryStandardName === companyName))
        .filter(item => !!item);
    return sortedDataSource?.[0] ?? dataSource?.[0];
};

/**
 * 获取薪资参考数据
 */
export default function useSalaryData({ positionCode, ready = true, cityDictValue, filterCompany = [] }) {
    const [dataSource, setDataSource] = React.useState([]);
    const [cityListLoading, cityList] = useCityList({ codes: ['linecity'] });
    const [loading, setLoading] = React.useState(false);

    const getSalaryData = async () => {
        setLoading(true);
        const { code, data, msg } = await getSalaryRef(positionCode);
        setLoading(false);
        if (code === 10000) {
            if (filterCompany.length) {
                setDataSource(
                    data
                        .filter(item => filterCompany.includes(item.salaryStandardName))
                        .map(item => ({ ...item, referencePostInfo: '[]' })) // 特别处理
                );
                return;
            }
            setDataSource(data);
        } else {
            message.error(msg);
        }
    };

    React.useEffect(() => {
        if (!ready) return;
        if (positionCode) {
            getSalaryData();
        } else {
            setDataSource([]);
        }
    }, [positionCode, ready]);

    // 当前使用的薪资数据
    const selectCity = cityList?.find(item => item.dictValue === cityDictValue) ?? cityList?.[0];
    const selectSalaryData = getSalaryValue(getSelectedSource(dataSource), selectCity?.remark);

    return {
        dataSource,
        targetCity: cityList,
        loading: loading || cityListLoading,
        selectCity,
        selectSalaryData,
        getSalaryData
    };
}
